package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.HolidayActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class HolidayRecycleStrategy extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.CrossHolidayActivity.getValue());
        if (openActivityInfoList == null) {
            return;
        }
        for (ActivityInfo activityInfo : openActivityInfoList) {
            if (ActivityMgr.activityInShowTime(activityInfo)) {
                long recycleTime = activityInfo.getEndTime() * 1000L + 600 * 1000;
                List<String> paramList = new ArrayList<>();
                List<Integer> list = StringUtils.stringToIntegerList(activityInfo.getParam4(), "\\|");
                Integer zhaoMuGoodsId = list.get(0);
                paramList.add(String.valueOf(list.get(1)));
                paramList.add(activityInfo.getActivityName());
                ActivityItemRecycleMgr.addRecycleItemData(zhaoMuGoodsId, activityType, 0, recycleTime, paramList);
            }
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.CrossHolidayActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        removeHolidayActivityBagItem(userId, data);
    }

    public void removeHolidayActivityBagItem(long userId, ActivityItemRecycleData data) {
        int goodsId = data.getItemId();
        Property reward = new Property();
        long count;
        BigInteger turnScore;
        UserBagItem userBagItem = getUserBagItem(userId,goodsId);
        if (null == userBagItem || userBagItem.getNum() <= 0) {
            return;
        }

        count = userBagItem.getNum();
        BigInteger petScore = GoodsMgr.getGoodsById(goodsId).getParamList().get(0);
        if (petScore.compareTo(BigInteger.ZERO) <= 0) {
            log.error("remove holiday activity goods to score,petScore config error,goodsId:{},remove count:{}", goodsId, count);
            return;
        }
        List<String> paramList = data.getParamList();
        turnScore = petScore.multiply(BigInteger.valueOf(count));
        reward.setGood(Integer.parseInt(paramList.get(0)), turnScore);
        String language = UserMgr.getUserInfo(userId).getLanguage();
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        String goodsName = "";
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
        if (null != goodsInfo) {
            goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(),language);
        } else {
            log.error("remove holiday activity goods to score,goods no find,goodsId:{},remove count:{}", goodsId, count);
        }
        if (removeItem(userBagItem)) {
            String activityName = paramList.get(1);
            activityName = ServerLanguageMgr.getContent(activityName,language);
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.HOLIDAY_ACTIVITY_GOODS_AUTO_CONSUME_TITLE, language),
                    activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.HOLIDAY_ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,language),
                    activityName, goodsName, count, turnScore);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
